/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file  SpiSlave.h
 * @brief Semidrive. AUTOSAR 4.3.1 MCAL SpiSlave_Slave plugins.
 */

#ifndef SPI_SLV_H
#define SPI_SLV_H

#ifdef __cplusplus
extern "C" {
#endif

/*******************************************************************************
**                      Include Section                                       **
*******************************************************************************/
#include "Compiler.h"
#include "SpiSlave_Types.h"
/*******************************************************************************
**                      Version Information                                   **
*******************************************************************************/
#define SPI_SLV_INSTANCE_ID SPI_SLV_INSTANCE_ID_VALUE

/* Version identification */
#define SPI_SLV_VENDOR_ID SPI_SLV_VENDOR_ID_VALUE
#define SPI_SLV_MODULE_ID SPI_SLV_MODULE_ID_VALUE
/*PRQA S 0791 10*/
/* AUTOSAR specification version information */
#define SPI_SLV_AR_RELEASE_MAJOR_VERSION 4U
#define SPI_SLV_AR_RELEASE_MINOR_VERSION 3U
#define SPI_SLV_AR_RELEASE_REVISION_VERSION 1U

/* File version information */
#define SPI_SLV_SW_MAJOR_VERSION SPI_SLV_SW_MAJOR_VERSION_VALUE
#define SPI_SLV_SW_MINOR_VERSION SPI_SLV_SW_MINOR_VERSION_VALUE
#define SPI_SLV_SW_PATCH_VERSION SPI_SLV_SW_PATCH_VERSION_VALUE

/*******************************************************************************
**                      Global Symbols                                        **
*******************************************************************************/
/*SpiSlave_Slave module levels */
#define SPI_SLV_LEVEL_0 0u
#define SPI_SLV_LEVEL_1 1u
#define SPI_SLV_LEVEL_2 2u

/*******************************************************************************
**                     Service IDs                                            **
*******************************************************************************/

/* Service ID for SpiSlave_Init() */
#define SPI_SLV_INIT_SID (uint8)0x00
/* Service ID for SpiSlave_DeInit() */
#define SPI_SLV_DEINIT_SID (uint8)0x01
/* Service ID for SpiSlave_WriteIB() */
#define SPI_SLV_WRITEIB_SID (uint8)0x02
/* Service ID for SpiSlave_AsyncTransmit() */
#define SPI_SLV_ASYNCTRANSMIT_SID (uint8)0x03
/* Service ID for SpiSlave_ReadIB() */
#define SPI_SLV_READIB_SID (uint8)0x04
/* Service ID for SpiSlave_SetupEB() */
#define SPI_SLV_SETUPEB_SID (uint8)0x05
/* Service ID for SpiSlave_GetStatus() */
#define SPI_SLV_GETSTATUS_SID (uint8)0x06
/* Service ID for SpiSlave_GetJobResult() */
#define SPI_SLV_GETJOBRESULT_SID (uint8)0x07
/* Service ID for SpiSlave_GetSequenceResult() */
#define SPI_SLV_GETSEQUENCERESULT_SID (uint8)0x08
/* Service ID for SpiSlave_GetVersionInfo() */
#define SPI_SLV_GETVERSIONINFO_SID (uint8)0x09
/* Service ID for SpiSlave_SyncTransmit() */
#define SPI_SLV_SYNCTRANSMIT_SID (uint8)0x0A
/* Service ID for SpiSlave_GetHWUnitStatus() */
#define SPI_SLV_GETHWUNITSTATUS_SID (uint8)0x0B
/* Service ID for SpiSlave_Cancel() */
#define SPI_SLV_CANCEL_SID (uint8)0x0C
/* Service ID for SpiSlave_SetAyncMode() */
#define SPI_SLV_SETAYNCMODE_SID (uint8)0x0D
/* Service ID for SpiSlave_MainFunction_Driving() */
#define SPI_SLV_MAINFUNCTION_HANDLING_SID (uint8)0x10

/*******************************************************************************
**                      DET Error Codes                                       **
*******************************************************************************/
/* API services invoked with invalid channel ID */
#define SPI_SLV_E_PARAM_CHANNEL (uint8)0x0A

/* API services invoked with invalid job ID */
#define SPI_SLV_E_PARAM_JOB (uint8)0x0B

/* API services invoked with invalid sequence ID */
#define SPI_SLV_E_PARAM_SEQ (uint8)0x0C

/* SpiSlave_SetupEB API invoked with length greater than configured length */
#define SPI_SLV_E_PARAM_LENGTH (uint8)0x0D

/* SpiSlave_GetHWUnitStatus API invoked with invalid hardware unit ID */
#define SPI_SLV_E_PARAM_UNIT (uint8)0x0E

/* SpiSlave_GetVersionInfo API invoked with invalid pointer */
#define SPI_SLV_E_PARAM_POINTER (uint8)0x10

/* API services invoked before the initialization of SPI Driver */
#define SPI_SLV_E_UNINIT (uint8)0x1A

/* SpiSlave_init API service called while the SpiSlave_Slave driver has already been initialized */
#define SPI_SLV_E_ALREADY_INITIALIZED (uint8)0x4A

/* API services invoked in a wrong sequence */
#define SPI_SLV_E_SEQ_PENDING (uint8)0x2A

/* SpiSlave_SyncTransmit API invoked at wrong time */
#define SPI_SLV_E_SEQ_IN_PROCESS (uint8)0x3A

/* SpiSlave_Slave ASyncTransmit error eg:fifo udr/ovr or dma err, */
#define SPI_SLV_E_ASYNC_ERROR (uint8)0x3B

/* SpiSlave_Slave cancel error eg:seq not in queue  */
#define SPI_SLV_E_CANCEL_ERROR (uint8)0x3C

/* multi core illegal access */
#define SPI_SLV_E_ILLEGAL_RES (uint8)0x3D
/** \brief  channel operation  not match */
#define SPI_SLV_E_CHANNEL_OPS (uint8)0x3E
/** \brief  channel operation  not align  */
#define SPI_SLV_E_CHANNEL_ALIGN (uint8)0x3F

/*******************************************************************************
**                      Global Data Types                                     **
*******************************************************************************/

/*******************************************************************************
**                      Function Prototypes                                   **
*******************************************************************************/
/**
 * @brief   Function init spi driver.
 *
 * @details Init spi controller base pre config types.
 *
 * @param   ConfigPtr spi config pointe.
 *
 * @return  NONE
 */
extern FUNC(void, SPI_SLV_CODE) SpiSlave_Init(P2CONST(SpiSlave_ConfigType, AUTOMATIC, SPI_SLV_APPL_CONST) ConfigPtr);

/**
 * @brief   Deinit spi driver.
 *
 * @details Deinit spi driver when driver in idle status.
 *
 * @param   ConfigPtr spi config pointe.
 *
 * @return  Std_ReturnType E_OK/E_NOT_OK
 */
extern FUNC(Std_ReturnType, SPI_SLV_CODE) SpiSlave_DeInit(void);

/**
 * @brief   Write prepare transmit data to internal buffer.
 *
 * @details Application data will be copy to spi interrnal buffer.
 *
 * @param   Channel spi channel index.
 *          DataBufferPtr pointe to application buffer.
 *
 * @return  Std_ReturnType E_OK/E_NOT_OK
 */
#if ((SPI_SLV_CHANNEL_BUFFERS_ALLOWED == 0x0) || (SPI_SLV_CHANNEL_BUFFERS_ALLOWED == 0x2))
extern FUNC(Std_ReturnType, SPI_SLV_CODE) SpiSlave_WriteIB(SpiSlave_ChannelType Channel,
        const SpiSlave_DataBufferType *DataBufferPtr);
#endif

/**
 * @brief   spi async transmit.
 *
 * @details transmit data use interrupt mode.
 *
 * @param   Sequence include jobs and channels.
 *
 * @return  Std_ReturnType E_OK/E_NOT_OK
 */
#if ((SPI_SLV_LEVEL_DELIVERED == SPI_SLV_LEVEL_1) || (SPI_SLV_LEVEL_DELIVERED == SPI_SLV_LEVEL_2))
extern FUNC(Std_ReturnType, SPI_SLV_CODE) SpiSlave_AsyncTransmit(SpiSlave_SequenceType Sequence);
#endif

/**
 * @brief   read data from spi internal buffer to application.
 *
 * @details read data from spi driver internal buffer to application buffer.
 *
 * @param   Channel spi channel index.
 *          DataBufferPointer pointe to application buffer.
 *
 * @return  Std_ReturnType E_OK/E_NOT_OK
 */
#if ((SPI_SLV_CHANNEL_BUFFERS_ALLOWED == 0x0) || \
     (SPI_SLV_CHANNEL_BUFFERS_ALLOWED == 0x2))
extern FUNC(Std_ReturnType, SPI_SLV_CODE) SpiSlave_ReadIB(SpiSlave_ChannelType Channel,
        SpiSlave_DataBufferType *DataBufferPointer);
#endif

/**
 * @brief   transmit/receive data use application buffer without copy.
 *
 * @details the data will be write to ro read from fifo directly.
 *
 * @param   Channel spi channel index.
 *          SrcDataBufferPtr pointe to buffer need be transmit.
 *          DesDataBufferPtr pointe to buffer need be receive.
 *          Length transmit data length.
 *
 * @return  Std_ReturnType E_OK/E_NOT_OK
 */
#if ((SPI_SLV_CHANNEL_BUFFERS_ALLOWED == 0x1) || \
     (SPI_SLV_CHANNEL_BUFFERS_ALLOWED == 0x2))
extern FUNC(Std_ReturnType, SPI_SLV_CODE) SpiSlave_SetupEB(SpiSlave_ChannelType Channel,
        const SpiSlave_DataBufferType *SrcDataBufferPtr,
        SpiSlave_DataBufferType *DesDataBufferPtr,
        SpiSlave_NumberOfDataType Length);
#endif

/**
 * @brief   return spi driver status.
 *
 * @details return spi driver status.
 *
 * @param   NONE.
 *
 * @return  SpiSlave_StatusType.
 */
extern FUNC(SpiSlave_StatusType, SPI_SLV_CODE) SpiSlave_GetStatus(void);

/**
 * @brief   return current spi job status.
 *
 * @details return current spi job status.
 *
 * @param   Job index.
 *
 * @return  SpiSlave_JobType.
 */
extern FUNC(SpiSlave_JobResultType, SPI_SLV_CODE)
SpiSlave_GetJobResult(SpiSlave_JobType Job);

/**
 * @brief   return current spi sequence status.
 *
 * @details return current spi sequence status.
 *
 * @param   Sequence index.
 *
 * @return  SpiSlave_SequenceType.
 */
extern FUNC(SpiSlave_SeqResultType, SPI_SLV_CODE)
SpiSlave_GetSequenceResult(SpiSlave_SequenceType Sequence);

/**
 * @brief   return spi driver verison.
 *
 * @details return spi driver verison.
 *
 * @param   versioninfo type.
 *
 * @return  NONE.
 */
#if (SPI_SLV_VERSION_INFO_API == STD_ON)
extern FUNC(void, SPI_SLV_CODE) SpiSlave_GetVersionInfo(Std_VersionInfoType *versioninfo);
#endif

/**
 * @brief   spi sync transmit.
 *
 * @details transmit data in polling mode, wait until data transmit finshed.
 *
 * @param   Sequence index.
 *
 * @return  Std_ReturnType E_OK/E_NOT_OK
 */
#if ((SPI_SLV_LEVEL_DELIVERED == SPI_SLV_LEVEL_0) || (SPI_SLV_LEVEL_DELIVERED == SPI_SLV_LEVEL_2))
extern FUNC(Std_ReturnType, SPI_SLV_CODE) SpiSlave_SyncTransmit(SpiSlave_SequenceType Sequence);
#endif

/**
 * @brief   return spi hardware unit status.
 *
 * @details return spi hardware unit status.
 *
 * @param   HWUnit index.
 *
 * @return  SpiSlave_StatusType.
 */
#if (SPI_SLV_HW_STATUS_API == STD_ON)
extern FUNC(SpiSlave_StatusType, SPI_SLV_CODE) SpiSlave_GetHWUnitStatus(SpiSlave_HWUnitType HWUnit);
#endif

/**
 * @brief   cancel spi transmit.
 *
 * @details cancel spi transmit when sequence transmit finshed.
 *
 * @param   Sequence index.
 *
 * @return  NONE.
 */
#if (SPI_SLV_CANCEL_API == STD_ON)
extern FUNC(void, SPI_SLV_CODE) SpiSlave_Cancel(SpiSlave_SequenceType Sequence);
#endif

/**
 * @brief   set async transmit mode.
 *
 * @details async mode: polling mode or interrupt mode.
 *
 * @param   Mode polling or interrupt.
 *
 * @return  Std_ReturnType E_OK/E_NOT_OK.
 */
#if (SPI_SLV_LEVEL_DELIVERED == SPI_SLV_LEVEL_2)
extern FUNC(Std_ReturnType, SPI_SLV_CODE) SpiSlave_SetAsyncMode(SpiSlave_AsyncModeType Mode);
#endif
/**
 * @brief   Async Transmition polling mian function.
 *
 * @details async mode: polling mode.
 *
 * @param   NONE.
 *
 * @return  NONE.
 */
#if (SPI_SLV_LEVEL_DELIVERED == SPI_SLV_LEVEL_1) || (SPI_SLV_LEVEL_DELIVERED == SPI_SLV_LEVEL_2)
extern FUNC(void, SPI_SLV_CODE) SpiSlave_MainFunction_Handling(void);
#endif
/**
 * @brief   Cancel the pending sequence.
 *
 * @details async mode: polling mode.
 *
 * @param   NONE.
 *
 * @return  NONE.
 */
#if (SPI_SLV_CANCEL_API == STD_ON)
extern FUNC(void, SPI_SLV_CODE) SpiSlave_Cancel(SpiSlave_SequenceType Sequence);
#endif
/**
 * @brief   Get the sequence transmit lenth.
 *
 * @details .
 *
 * @param   NONE.
 *
 * @return  NONE.
 */
extern FUNC(sint32, SPI_SLV_CODE) SpiSlave_GetTransmittedBytes(SpiSlave_SequenceType SequenceId);
/*******************************************************************************
**                      Global Data                                           **
*******************************************************************************/
#ifdef __cplusplus
}
#endif

#endif /* SPI_SLV_H */

/*******************************************************************************
**                      End of File                                           **
*******************************************************************************/
